package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.BasDict;
import com.czkt.pojo.SysUser;
import com.czkt.service.IBasDictService;
import com.czkt.utils.Md5Utils;
import com.czkt.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/dict")
public class BasDictController {
    @Autowired
    IBasDictService basDictService;

    @RequestMapping("/list")
    public Object list(BasDict basDict, @RequestParam(defaultValue = "1") Integer pageIndex,@RequestParam(defaultValue = "2") Integer pageSize, Model model){

        QueryWrapper<BasDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasDict::getDictType,"客户等级");
        queryWrapper.lambda().eq(basDict.getDictId() != null,BasDict::getDictId,basDict.getDictId());
        queryWrapper.lambda().like(StringUtils.hasText(basDict.getDictItem()),BasDict::getDictItem,basDict.getDictItem());

        IPage<BasDict> page = new Page<>(pageIndex,pageSize);
        page = basDictService.page(page,queryWrapper);

        PageUtils pageUtils = new PageUtils();

        BeanUtils.copyProperties(page,pageUtils);

        model.addAttribute("dictPage",pageUtils);
        model.addAttribute("dictId",basDict.getDictId());
        model.addAttribute("dictItem",basDict.getDictItem());
        return "dict/list";
    }

    @RequestMapping("/add")
    public Object add(){
        return "dict/add";
    }

    @RequestMapping("/save")
    public Object save(BasDict basDict){
        if (basDictService.saveOrUpdate(basDict)){
            return "redirect:dict/list";
        } else if (basDict.getDictId() != 0){
            return "dict/edit";
        } else {
            return "redirect:dict/add";
        }
    }

    @RequestMapping("/edit")
    public Object edit(Long dictId,Model model){
        model.addAttribute("dict",basDictService.getById(dictId));
        return "dict/edit";
    }

}